/*   Shotscreens
 *   Copyright (C) 2010, Vadim Zakondyrin (http://crystalnix.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef QGRAPHICSPAINTER_H
#define QGRAPHICSPAINTER_H

#include <QList>
#include <QPointF>
#include <QPen>
#include <QGraphicsView>
#include "paintmodes.h"
#include "../paintproperties.h"
#include "qgraphicsbound.h"

class QGraphicsItem;
class QGraphicsScene;
class QGraphicsBrushItem;
class QGraphicsLineItem;
class QGraphicsArrowItem;
class QGraphicsTextRectItem;

class QGraphicsPainter : public QGraphicsView
{
    Q_OBJECT;
public:
    QGraphicsPainter(QWidget * parent = 0);
    QGraphicsPainter(QGraphicsScene * scene, QWidget * parent = 0);
    ~QGraphicsPainter();

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void closeEvent(QCloseEvent *);
private:
    QList<QGraphicsItem *> graphicsItems;
    QGraphicsBrushItem * brushItem;
    QGraphicsLineItem * lineItem;
    QGraphicsArrowItem * arrowItem;
    QGraphicsTextRectItem * textItem;
    QGraphicsRectItem *rectangle;
    QGraphicsBound * selectedItem;
    QGraphicsTextItem *item;
    bool holdLeftButton;
    QPointF firstPoint;
    QString textForPaint;
    PaintModes::PaintMode m_paintMode;
    PaintProperties paintproperties;
    int is_need_draw;
    int is_out_area;
    void initializationPainter();

    void addItem(QGraphicsItem * newItem);
    void addArrowItem(const QPointF &pos);
    void addLineItem(const QPointF &pos);
    void addBrushItem(const QPointF &pos);
    void addTextItem(const QPointF &pos);
    void AddRectangle(const QPointF &rect);
    void selectItemByPoint(const QPointF &pos, bool next = false);
    QRect gr_rect;
    QRect picture_rect;

    int undo_item_index;
    //QPoint undo_item_pos;
    QList<QPoint> undo_item_pos;

    bool redo_enabled;
    //My addition
    QList<QGraphicsItem *> removedItems;

public:
    inline PaintModes::PaintMode paintMode() { return m_paintMode; }
    inline void setPaintMode(const PaintModes::PaintMode & paintMode) { m_paintMode = paintMode; }

    inline QString getTextForPaint() { return textForPaint; }
    inline void setTextForPaint(const QString & text) { textForPaint = text; }

    void showPaintProperties();

    inline bool isEmpty() { return graphicsItems.isEmpty(); }

    void undo();
    void Redo();
    void clearAll();
    void SetRealRect(QRect rect){ gr_rect=rect ;}
    QRect GetRealRect(){return gr_rect; }
    bool IsInNormalRect(int x,int y);
    bool CanRedo(){return redo_enabled;}
    bool IsSceneEmpty();
signals:
    void undoEnabled(bool);
public slots:
    void zoomIn(){scale(1.2,1.2);}
    void zoomOut(){scale(1 / 1.2, 1 / 1.2);}
    void ZoomReset(){resetMatrix();}


};

#endif // QGRAPHICSPAINTER_H
